import Vue from 'vue'
// eslint-disable-next-line no-unused-vars
import VueRouter from 'vue-router';

import router from './router';

import App from './App.vue'
import axios from "axios";
import store from "./store";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.css'

import {postRequest} from "@/utils/api";
import {putRequest} from "@/utils/api";
import {getRequest} from "@/utils/api";
import {deleteRequest} from "@/utils/api";
import {initMenu} from "@/utils/menus";
import {downloadRequest} from "@/utils/download";
Vue.use(ElementUI)

Vue.prototype.postRequest=postRequest;
Vue.prototype.putRequest=putRequest;
Vue.prototype.getRequest=getRequest;
Vue.prototype.deleteRequest=deleteRequest;
Vue.prototype.downloadRequest = downloadRequest;

//路由守卫
router.beforeEach((to,from,next)=>{
  if(window.sessionStorage.getItem('tokenStr')){
    initMenu(router,store);
    //判断用户信息是否存在
    if(!window.sessionStorage.getItem('user')){
      return getRequest('/admin/info').then(resp=>{
        if(resp){
          //存入用户信息
          window.sessionStorage.setItem('user',JSON.stringify(resp));
          store.commit('INIT_ADMIN',resp);
          next();
        }
      })
    }
    next();
  }else {
    if (to.path=='/'){
      next();
    }else {
      next('/?redirect='+to.path);
    }

  }
})

Vue.prototype.$axios = axios;
//使用Vue-Router
Vue.use(VueRouter);
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')
